<template>
  <el-dialog
    title="游戏流水"
    :visible.sync="visible"
    width="60%"
    top="10vh"
    :before-close="closeDialogHandle"
    :close-on-click-modal="false"
    class="game-flow"
  >
    <el-form size="mini" inline :model="searchForm" ref="searchForm" @keyup.enter.native="searchHandle" label-width="80px">
      <el-form-item prop="prizeId" label="游戏奖品">
        <el-select v-model="searchForm.prizeId" clearable filterable placeholder="请选择游戏奖品">
          <el-option v-for="gamePrize in gamePrizeList" :key="gamePrize.value"
                     :label="gamePrize.name" :value="gamePrize.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="prizeId" label="游戏用户">
        <el-select v-model="searchForm.userId" clearable filterable placeholder="请选择游戏用户">
          <el-option v-for="gameUser in gameUserList" :key="gameUser.value"
                     :label="gameUser.name" :value="gameUser.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="startTime" label="开始时间">
        <el-date-picker
          v-model="searchForm.startTime"
          type="datetime"
          placeholder="请选择开始时间"
          default-time="00:00:00"
          value-format="timestamp">
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="endTime" label="结束时间">
        <el-date-picker
          v-model="searchForm.endTime"
          type="datetime"
          placeholder="请选择结束时间"
          default-time="23:59:59"
          value-format="timestamp">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="dataLoad" @click="searchHandle" type="primary" icon="el-icon-search">查询</el-button>
        <el-button :disabled="dataLoad" @click="resetFormHandle" plain icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataLoad"
      element-loading-text="游戏流水列表加载中..."
      element-loading-spinner="el-icon-loading"
      :data="gameFlowList"
      ref="gameFlowTable"
    >
      <el-table-column prop="prizeName" header-align="center" align="center" label="抽中奖品"></el-table-column>
      <el-table-column prop="finalPrizeName" header-align="center" align="center" label="实际发放奖品">
        <template v-slot="{row}">
          <span>{{ row.finalPrizeName }}</span>
          <span v-if="row.prizeName !== row.finalPrizeName" class="danger-color">（不一致）</span>
        </template>
      </el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="游戏用户"></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :disabled="dataLoad"
      :current-page="searchForm.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="searchForm.limit"
      :total="totalNum"
      layout="total, sizes, prev, pager, next, jumper"
      class="paging">
    </el-pagination>
  </el-dialog>
</template>

<script>

  export default {
    data() {
      return {
        // 弹出框显示状态[true-显示 false-隐藏]
        visible: false,
        // 游戏流水检索表单对象
        searchForm: {
          // 游戏主键
          gameId: undefined,
          // 奖品主键
          prizeId: '',
          // 用户主键
          userId: '',
          // 创建时间（开始）
          startTime: '',
          // 创建时间（结束）
          endTime: '',
          // 当前展示页码
          page: 1,
          // 当前页显示个数
          limit: 10,
        },
        // 游戏流水列表
        gameFlowList: [],
        // 游戏流水总数
        totalNum: 0,
        // 游戏奖品列表
        gamePrizeList: [],
        // 游戏用户列表
        gameUserList: [],
        // 是否正在加载数据[true-是 false-不是]
        dataLoad: false,
      }
    },
    methods: {
      /**
       * 初始化弹出框方法
       * @param {string} gameId 游戏主键
       */
      initDialog(gameId) {
        console.log('gameId', gameId);
        // 打开弹出框
        this.visible = true;

        this.searchForm.gameId = gameId;

        this.getPrizeList();
        this.getUserList();
        this.getGameFlowList();
      },
      /**
       * 搜索游戏流水事件
       */
      searchHandle() {
        this.searchForm.page = 1;
        this.getGameFlowList();
      },
      /**
       * 获取游戏流水方法
       */
      getGameFlowList() {
        // 开始加载
        this.dataLoad = true;
        this.$axios.$post("/game-flow/page/list", this.searchForm)
          .then(({ data }) => {
            this.gameFlowList = data.data;
            this.totalNum = data.totalNum;
          })
          .catch(() => {})
          .finally(() => {
            // 加载结束
            this.dataLoad = false;
          });
      },
      /**
       * 获取游戏奖品列表方法
       */
      getPrizeList() {
        this.$axios.$get('/game-prize/simple/list', { params: { gameId: this.searchForm.gameId }})
          .then(({ data }) => {
            this.gamePrizeList = data;
          });
      },
      /**
       * 获取游戏用户列表方法
       */
      getUserList() {
        this.$axios.$get('/game-user/simple/list', { params: { gameId: this.searchForm.gameId }})
          .then(({ data }) => {
            this.gameUserList = data;
          });
      },
      /**
       * 每页显示个数变化事件
       * @param {number} size 最新显示个数值
       */
      sizeChangeHandle(size) {
        let searchForm = this.searchForm;
        searchForm.page = 1;
        searchForm.limit = size;
        this.getGameFlowList();
      },
      /**
       * '点击特定页码'、'上一页'、'下一页'操作事件
       * @param {number} index 最新页码
       */
      currentChangeHandle(index) {
        let searchForm = this.searchForm;
        if (searchForm.page === index) {
          return;
        }
        searchForm.page = index;
        this.getGameFlowList();
      },
      /**
       * 重置搜索表单对象事件
       */
      resetFormHandle() {
        // 重置表单对象
        this.$refs.searchForm.resetFields();
        this.searchForm.page = 1;
      },
      /**
       * 关闭弹出框事件
       */
      closeDialogHandle() {
        // 重置表单对象
        this.$refs.searchForm.resetFields();
        this.searchForm.page = 1;
        this.searchForm.limit = 10;
        // 游戏流水列表
        this.gameFlowList = []
        this.totalNum = 0
        this.gamePrizeList = []
        this.gameUserList = []

        this.$nextTick(() => {
          // 关闭弹出框
          this.visible = false;
        })
      }
    }
  }
</script>

<style scoped>
  .game-flow .paging {
    text-align: right;
    margin: 7px;
  }
</style>
